package run.siyuan.jdk.测试;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * {@code ClassName:} StringTest
 * {@code Description:} TODO
 * {@code Author:} siyuan
 * {@code Date:} 2023/3/14 11:34
 */
public class StringTest extends Thread {

    static final CountDownLatch end = new CountDownLatch(2);

    private String str = null;

    public StringTest() {
    }

    public StringTest(String str) {
        this.str = str;
    }

    @Override
    public void run() {
        String a = new StringBuffer(str).reverse().toString();
        //System.out.println(a);a
        str = a;
        end.countDown();
    }

    public String getStr() {
        return str;
    }


    public void reverseStrings(String a, String b) {
        try {
            List<String> list = List.of(a, b);
            StringTest[] threads = new StringTest[2];
            for (int i = 0; i < list.size(); i++) {
                threads[i] = new StringTest(list.get(i));
                threads[i].start();
            }
            end.await();

            for (int i = 0; i < threads.length; i++) {
                System.out.println(threads[i].getStr());
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


    }

    public static void main(String[] args) {
        StringTest test = new StringTest();
        test.reverseStrings("123456", "abcde");
    }
}
